import sys
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QLabel, QLineEdit, QPushButton, QTextEdit, QFrame
)
from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtGui import QFont, QTextCursor

class ParameterProcessor(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("参数处理器")
        self.setGeometry(100, 100, 800, 600)
        
        # 设置主窗口样式
        self.setStyleSheet("""
            QMainWindow {
                background-color: #2c3e50;
            }
            QLabel {
                color: #ecf0f1;
                font-weight: bold;
            }
            QLineEdit {
                background-color: #34495e;
                color: #ecf0f1;
                border: 1px solid #7f8c8d;
                border-radius: 5px;
                padding: 5px;
            }
            QPushButton {
                background-color: #3498db;
                color: white;
                border: none;
                border-radius: 5px;
                padding: 10px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #2980b9;
            }
            QPushButton:pressed {
                background-color: #1d6fa5;
            }
            QTextEdit {
                background-color: #34495e;
                color: #ecf0f1;
                border: 1px solid #7f8c8d;
                border-radius: 5px;
                font-family: Consolas, Courier New, monospace;
            }
            QFrame {
                background-color: #34495e;
                border-radius: 10px;
                padding: 15px;
            }
        """)
        
        self.init_ui()
        
    def init_ui(self):
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setSpacing(20)
        main_layout.setContentsMargins(20, 20, 20, 20)
        
        # 标题标签
        title_label = QLabel("参数处理器")
        title_label.setFont(QFont("Arial", 18, QFont.Bold))
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("color: #3498db;")
        main_layout.addWidget(title_label)
        
        # 输入区域框架
        input_frame = QFrame()
        input_frame.setFrameShape(QFrame.StyledPanel)
        input_layout = QVBoxLayout(input_frame)
        input_layout.setSpacing(15)
        
        # 参数1输入
        param1_layout = QHBoxLayout()
        param1_label = QLabel("参数 1:")
        param1_label.setFont(QFont("Arial", 10))
        self.param1_input = QLineEdit()
        self.param1_input.setPlaceholderText("输入第一个参数...")
        param1_layout.addWidget(param1_label)
        param1_layout.addWidget(self.param1_input)
        input_layout.addLayout(param1_layout)
        
        # 参数2输入
        param2_layout = QHBoxLayout()
        param2_label = QLabel("参数 2:")
        param2_label.setFont(QFont("Arial", 10))
        self.param2_input = QLineEdit()
        self.param2_input.setPlaceholderText("输入第二个参数...")
        param2_layout.addWidget(param2_label)
        param2_layout.addWidget(self.param2_input)
        input_layout.addLayout(param2_layout)
        
        # 添加输入框架到主布局
        main_layout.addWidget(input_frame)
        
        # 处理按钮
        self.process_btn = QPushButton("处理参数")
        self.process_btn.setFont(QFont("Arial", 12))
        self.process_btn.setFixedHeight(45)
        self.process_btn.clicked.connect(self.process_inputs)
        main_layout.addWidget(self.process_btn)
        
        # 日志区域框架
        log_frame = QFrame()
        log_frame.setFrameShape(QFrame.StyledPanel)
        log_layout = QVBoxLayout(log_frame)
        
        log_label = QLabel("操作日志:")
        log_label.setFont(QFont("Arial", 10))
        log_layout.addWidget(log_label)
        
        # 日志文本框
        self.log_area = QTextEdit()
        self.log_area.setReadOnly(True)
        self.log_area.setFont(QFont("Consolas", 10))
        log_layout.addWidget(self.log_area)
        
        # 添加日志框架到主布局
        main_layout.addWidget(log_frame, 1)  # 1 表示占用更多空间
        
        # 添加初始日志
        self.add_log("系统已启动，等待输入...")
        
    def process_inputs(self):
        """处理输入参数并更新日志"""
        param1 = self.param1_input.text().strip()
        param2 = self.param2_input.text().strip()
        
        if not param1 or not param2:
            self.add_log("错误：两个参数都需要输入！", "error")
            return
            
        # 获取当前时间
        timestamp = QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
        
        # 处理逻辑（这里简单拼接参数作为示例）
        result = f"处理结果: {param1} + {param2} = {param1 + param2}"
        
        # 更新日志
        self.add_log(f"[{timestamp}] 输入1: {param1}")
        self.add_log(f"[{timestamp}] 输入2: {param2}")
        self.add_log(f"[{timestamp}] {result}")
        self.add_log("-" * 50)
        
        # 清空输入框
        self.param1_input.clear()
        self.param2_input.clear()
        
    def add_log(self, message, log_type="info"):
        """向日志区域添加消息"""
        # 根据日志类型设置颜色
        if log_type == "error":
            color = "#e74c3c"  # 红色
        elif log_type == "success":
            color = "#2ecc71"  # 绿色
        else:
            color = "#3498db"  # 蓝色
            
        # 添加带颜色的日志消息
        self.log_area.moveCursor(QTextCursor.End)
        self.log_area.setTextColor(color)
        self.log_area.append(message)
        
        # 自动滚动到底部
        self.log_area.ensureCursorVisible()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ParameterProcessor()
    window.show()
    sys.exit(app.exec_())
